<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Pagination.php';
require_once 'CMS/Language.php'; //include thu vien languages

//Người tạo : Nguyễn Hồng Danh - Team 2
//Module 	: DescriptionNews
//Chức năng	: Show ra mô tả của tin tức
 
class ListShow_IndexController {
	private $pagination;
	public function init(){
		parent::init();
	}

	public function indexAction(){
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/listshow');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
		
		/*========== Start Get Text ==============*/
		$this->view->latestqa		= $language->get('text_latestqa');
		$this->view->latestproduct		= $language->get('text_latestproduct');
		$this->view->latestnews		= $language->get('text_latestnews');
		$this->view->show			= $language->get('text_show');
		$this->view->update			= $language->get('text_update');
		$this->view->pi				= $language->get('text_pi');
		$this->view->price			= $language->get('text_price');
		$this->view->detail			= $language->get('text_detail');
		$this->view->nothing		= $language->get('text_nothing');
		$this->view->cdate			= $language->get('text_cdate');
		$this->view->view			= $language->get('text_view');
		$this->view->btndetail			= $language->get('text_prodetail');
		$this->view->btnadd				= $language->get('text_add');	
		/*========== End Text ==============*/
		
		//Start------Khai báo các parameter
		$path = APPLICATION_PATH."/widgets/listshow/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		//End--------Khai báo các parameter
		
		/************ Start check user login *********/
		$session1 = new Zend_Session_Namespace('login.auth');
		if(isset($session1->id_user))
		{
			$this->view->login = "1";
			/************ Start check permission *********/
			$user = new User();
			$user->init();
			$hasmodify = $user->modifiy_content($xmlconfig->name);
			$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else 
		{
			
			$this->view->login = "0";
		}
		/************ End check user login *********/
		
		$this->pagination = new Pagination();
		if(isset($_GET["show"]) && $_GET["show"]==1) // show dang detail
		{
			//-----------------------Khai báo các parameter----------------------//
			$tbThings= new ListShow_Model_Things();
			$tbCategories= new ListShow_Model_Categories();
			$dateInt = new Util();
			$type="all";//Khỏi tạo type là hiển thị tất cả sản phẩm không theo một category nào
			//---------------------- Khai báo các parameter----------------------//
			
			//Start------Get Products List
			$dataProducts=0;
			$category=0;
			$dataNews=0;
			
	
				if(count($_GET)&&isset($_GET["type"]))// nếu ng dùng chọn xem tin or xem sp
				{
					if($dateInt->is_interger($_GET["type"]))
					{
						$loai=$_GET["type"];
						if($loai==2)// news
						{
							$type="news";
							$dataProducts=$tbThings->getAllThingsbyType($loai);
							if(isset($_GET["category"]))
							{
								if($dateInt->is_interger($_GET["category"]))
								{
								$category=$_GET["category"];
								
								//Edit
								
								$category_info=$tbCategories->getCategoriesbyId($category);
								if(count($category_info)>0) $this->view->cate_info = $category_info[0];
								
								//Start------get name of category
								$nameCate = unserialize($category_info[0]['category_name']);
								if(isset($nameCate[$name['name']]) && isset($name)){
									$category_name = $nameCate[$name['name']];
								}else{
									$category_name = $nameCate['Default'];
								}
								$this->view->cate_name = $category_name;
								//End--------get name of category
								
								if($category_info[0]['is_leaf']==1) //nếu là nút lá
								{	
									$dataProducts=$tbThings->getTypedThingsByCategory($category,2);//Root
									$this->view->categoryleaf = $category;
								}
								else //là nút cha
								{
									$child_categories=$tbCategories->getLeafCategoriesByParent($category);//get tất cả nút con
									$count=0;
									$dataProducts=array();
									if($child_categories!=null)
									{
										foreach($child_categories as $child_category)
										{
										
											$products=$tbThings->getTypedThingsByCategory($child_category['category_id'],2);
											if(count($products)>0)
											{
												foreach($products as $product)
													array_push($dataProducts,$product);
											}
											$count++;
										}
									}
								}
								
								//End Edit
								$type="news_category";
								}
								else 
								{
									$this->_redirect('/index/index');
								}
							}
							
						
						
						
						}
						if($loai==1)// product
						{
							$type="all";
							$dataProducts=$tbThings->getAllThingsbyType(1);
							if(isset($_GET["category"]))
								{
									if($dateInt->is_interger($_GET["category"]))
									{
										$category=$_GET["category"];
										//Edit
										$category_info=$tbCategories->getCategoriesbyId($category);
										if(count($category_info)>0) $this->view->cate_info = $category_info[0];
										
										//Start------get name of category
										$nameCate = unserialize($category_info[0]['category_name']);
										if(isset($nameCate[$name['name']]) && isset($name)){
											$category_name = $nameCate[$name['name']];
										}else{
											$category_name = $nameCate['Default'];
										}
										$this->view->cate_name = $category_name;
										//End--------get name of category
										
										if($category_info[0]['is_leaf']==1) //nếu là nút lá
										{
											$dataProducts = $tbThings->getTypedThingsByCategory($category,1);//Root
											
											$this->view->categoryleaf = $category;
										}
										else //là nút cha
										{
											$child_categories=$tbCategories->getLeafCategoriesByParent($category);//get tất cả nút con
											$count=0;
											$dataProducts=array();
											if($child_categories!=null)
											{
												foreach($child_categories as $child_category)
												{
													
													$products=$tbThings->getTypedThingsByCategory($child_category['category_id'],1);
													if(count($products)>0)
													{
														foreach($products as $product)
															array_push($dataProducts,$product);
													}
													
													$count++;
												}
											}
										}
										//End Edit
										$type="product_category";
										
									}
									else 
									{
										$this->_redirect('/index/index');
									}
								}
						}
						if($loai==3)// Qa
						{
							$type="QA";
							$dataProducts=$tbThings->getAllThingsbyType(3);
							if(isset($_GET["category"]))
								{
									if($dateInt->is_interger($_GET["category"]))
									{
									$category=$_GET["category"];
									
									//Edit
								
									$category_info=$tbCategories->getCategoriesbyId($category);
									if(count($category_info)>0) $this->view->cate_info = $category_info[0];
									
									//Start------get name of category
									$nameCate = unserialize($category_info[0]['category_name']);
									if(isset($nameCate[$name['name']]) && isset($name)){
										$category_name = $nameCate[$name['name']];
									}else{
										$category_name = $nameCate['Default'];
									}
									$this->view->cate_name = $category_name;
									//End--------get name of category
									
									if($category_info[0]['is_leaf']==1) //nếu là nút lá
									{
										$dataProducts=$tbThings->getTypedThingsByCategory($category,3);//Root
										$this->view->categoryleaf = $category;
									}
									else //là nút cha
									{
										$child_categories=$tbCategories->getLeafCategoriesByParent($category);//get tất cả nút con
										$count=0;
										$dataProducts=array();
										if($child_categories!=null)
										{
											foreach($child_categories as $child_category)
											{
										
												$products=$tbThings->getTypedThingsByCategory($child_category['category_id'],3);
												if(count($products)>0)
												{
													foreach($products as $product)
														array_push($dataProducts,$product);
												}
												$count++;
											}
										}
									}
								
									//End Edit
									$type="QA_category";
									}
									else 
									{
									$this->_redirect('/index/index');
									}
								}
						}
					}
					else 
					{
						$this->_redirect('/index/index');
					}
				}
				else
				{
					$dataProducts=$tbThings->$tbThings->getAllThingsbyType(1);
						$type="all";
				}
			
			
			
			
			
			if($dataProducts!=null)
			{
				//Start------Get số lượng Products
				$countProducts=count($dataProducts);
				//End---------Get Product
				
				// kiem tra xem dang o page may
				if (!isset($_GET["page"]) || is_numeric($_GET["page"])!=true ||$_GET["page"]<0) 
		            $page = 1;
		        else 
		            $page = $_GET["page"];
		            	
		        // so product tren 1 trang
		        $limit = 4;
		            
		        // khoi tao phan trang
		       
		        switch ($type) {
		        	case "news":{
		        		$this->pagination->init("?show=1&type=".$loai."&page=",$countProducts,$limit,$page);
		        		
		        		break;
		        	}
		        	case "news_category":{
		        		$this->pagination->init("?show=1&type=".$loai."&category=".$category."&page=", $countProducts, $limit, $page);
		        		break;
		        	
		        	}
		        	case "QA":{
		        		$this->pagination->init("?show=1&type=".$loai."&page=",$countProducts,$limit,$page);
		        		
		        		break;
		        	}
		        case "QA_category":{
		        		$this->pagination->init("?show=1&type=".$loai."&category=".$category."&page=",$countProducts,$limit,$page);
		        		
		        		break;
		        	}
		        	case "product_category":{
		        		$this->pagination->init("?show=1&type=".$loai."&category=".$category."&page=", $countProducts, $limit, $page);
		        		break;
		        	}
		        	
		        	default:{
		        		$this->pagination->init("?show=1&page=", $countProducts, $limit, $page);
		        		
		        		break;
		        	}
		        	
		        }
		        
				// phan trang ban ajax
		       $this->view->pagination = $this->pagination->render();
				
				switch ($type)
				{
					case "all":
						{
							$dataProducts=$tbThings->getThingsInPage(1,$page,$limit);
							break;
						}
					case "news":
						{
							
						}
					
					case "QA":
						{
							$dataProducts=$tbThings->getThingsInPage($loai,$page, $limit);
							break;
						}
					case "news_category":
						{
						}	
					case "QA_category":
						{
							
						}	
					
					case "product_category":
						{
							$temp=$dataProducts;
							$dataProducts=array();
							for($i=($page-1)*$limit;$i<$page*$limit;$i++)
							{
								if($i>=count($temp))
									break;
								array_push($dataProducts,$temp[$i]);
							}
							break;
						}
				}
				
			//format multi-language
			switch ($type)
			{
				case "all":{}
				case "product_category":
				{
					$i=0;
					if($dataProducts!=null)
					{
						foreach($dataProducts as $product)
						{
							$description_lang=unserialize($product["short_description"]);
							if(isset($description_lang[$name['name']]))
								$product["short_description"]=$description_lang[$name['name']];
							else
								$product["short_description"]="-------------------";
								
							$Products[$i]=array(
							"thing_id"=>$product["thing_id"],
							"title"=>$product["title"],
							"short_description"=>$product["short_description"],
							"price"=>$product["price"],
							"update_date" =>$dateInt->makeDays($product['update_date']),
							"avatar"=>$product["avatar"],
							);
							$i++;
						}
					}
					break;
				}
				case "news":{}
				case "QA":{}
				case "QA_category":{}
				case "news_category":
				{
					$i=0;
					if($dataProducts!=null)
					{
						foreach($dataProducts as $product)
						{
							$description_lang=unserialize($product["short_description"]);
							$title_lang=unserialize($product["title"]);
							if(isset($description_lang[$name['name']]))
								$product["short_description"]=$description_lang[$name['name']];
							else
								$product["short_description"]="-------------------";
							if(isset($title_lang[$name['name']]))
								$product["title"]=$title_lang[$name['name']];
							else
								$product["title"]="-------------------";
							
							$Products[$i]=array(
							"thing_id"=>$product["thing_id"],
							"title"=>$product["title"],
							"short_description"=>$product["short_description"],
							"price"=>$product["price"],
							"update_date" =>$dateInt->makeDays($product['update_date']),
							"avatar"=>$product["avatar"],
							);
							$i++;
						}
					}
					break;
				}
			}
				
				
				//Start------Truyền parameter qua View
				switch ($type)
				{
					
					case "news":
						{
							
							
						}
				case "QA":
						{
							
							
						}
				case "QA_category":
						{
							
							
						}
					case "news_category":
						{
							$this->view->News=$Products;
							break;
						}
					
				case "all":
						{
							
						
						}
				case "product_category":
						{
							$this->view->Products=$Products;
							break;
						}
				}
				
				$this->view->countProducts=$countProducts;
				//End--------Truyền parameter qua View
			}
			//End------Get Products List
		}
		/////////////////////////////////////////////////////////////////////////
		//                                                                     //
		/////////////////////////////////////////////////////////////////////////
		else if(isset($_GET["show"]) && $_GET["show"]==2) // show dang short
		{
			//Start------Khai báo các parameter
			$tbCategories = new ListShow_Model_Categories();
			$tbThings = new ListShow_Model_Things();
			$dateInt = new Util();
			//End--------Khai báo các parameter
			
			$page = 1;
			$limit = 10;
			$type = "all";
			$things = 0;
			$category = 0;
			$count = 0;
			
			if(count($_GET)&&isset($_GET["type"]))// nếu ng dùng chọn xem tin or xem sp
				{
					if($dateInt->is_interger($_GET["type"]))
					{
						$loai=$_GET["type"];
						if($loai==2)// news
						{
							$type="news";
							$dataProducts=$tbThings->getAllThingsbyType($loai);
							if(isset($_GET["category"]))
							{
								if($dateInt->is_interger($_GET["category"]))
								{
								$category=$_GET["category"];
								
								//Edit
								
								$category_info=$tbCategories->getCategoriesbyId($category);
								if(count($category_info)>0) $this->view->cate_info = $category_info[0];
								
								//Start------get name of category
								$nameCate = unserialize($category_info[0]['category_name']);
								if(isset($nameCate[$name['name']]) && isset($name)){
									$category_name = $nameCate[$name['name']];
								}else{
									$category_name = $nameCate['Default'];
								}
								$this->view->cate_name = $category_name;
								//End--------get name of category
								
								if($category_info[0]['is_leaf']==1) //nếu là nút lá
								{	
									$dataProducts=$tbThings->getTypedThingsByCategory($category,2);//Root
									$this->view->categoryleaf = $category;
								}
								else //là nút cha
								{
									$child_categories=$tbCategories->getLeafCategoriesByParent($category);//get tất cả nút con
									$count=0;
									$dataProducts=array();
									if($child_categories!=null)
									{
										foreach($child_categories as $child_category)
										{
										
											$products=$tbThings->getTypedThingsByCategory($child_category['category_id'],2);
											if(count($products)>0)
											{
												foreach($products as $product)
													array_push($dataProducts,$product);
											}
											$count++;
										}
									}
								}
								
								//End Edit
								$type="news_category";
								}
								else 
								{
									$this->_redirect('/index/index');
								}
							}
							
						
						
						
						}
						if($loai==1)// product
						{
							$type="all";
							$dataProducts=$tbThings->getAllThingsbyType(1);
							if(isset($_GET["category"]))
								{
									if($dateInt->is_interger($_GET["category"]))
									{
										$category=$_GET["category"];
										//Edit
										$category_info=$tbCategories->getCategoriesbyId($category);
										if(count($category_info)>0) $this->view->cate_info = $category_info[0];
										
										//Start------get name of category
										$nameCate = unserialize($category_info[0]['category_name']);
										if(isset($nameCate[$name['name']]) && isset($name)){
											$category_name = $nameCate[$name['name']];
										}else{
											$category_name = $nameCate['Default'];
										}
										$this->view->cate_name = $category_name;
										//End--------get name of category
										
										if($category_info[0]['is_leaf']==1) //nếu là nút lá
										{
											$dataProducts=$tbThings->getTypedThingsByCategory($category,1);//Root
											$this->view->categoryleaf = $category;
										}
										else //là nút cha
										{
											$child_categories=$tbCategories->getLeafCategoriesByParent($category);//get tất cả nút con
											$count=0;
											$dataProducts=array();
											if($child_categories!=null)
											{
												foreach($child_categories as $child_category)
												{

													$products=$tbThings->getTypedThingsByCategory($child_category['category_id'],1);
													if(count($products)>0)
													{
															foreach($products as $product)
																array_push($dataProducts,$product);
													}
													$count++;
												}
											}
										}
										//End Edit
										$type="product_category";
										
									}
									else 
									{
										$this->_redirect('/index/index');
									}
								}
						}
						if($loai==3)// Qa
						{
							$type="QA";
							$dataProducts=$tbThings->getAllThingsbyType(3);
							if(isset($_GET["category"]))
								{
									if($dateInt->is_interger($_GET["category"]))
									{
									$category=$_GET["category"];
									
									//Edit
								
									$category_info=$tbCategories->getCategoriesbyId($category);
									if(count($category_info)>0) $this->view->cate_info = $category_info[0];
								
									//Start------get name of category
									$nameCate = unserialize($category_info[0]['category_name']);
									if(isset($nameCate[$name['name']]) && isset($name)){
										$category_name = $nameCate[$name['name']];
									}else{
										$category_name = $nameCate['Default'];
									}
									$this->view->cate_name = $category_name;
									//End--------get name of category
								
									if($category_info[0]['is_leaf']==1) //nếu là nút lá
									{
										$dataProducts=$tbThings->getTypedThingsByCategory($category,3);//Root
										$this->view->categoryleaf = $category;
									}
									else //là nút cha
									{
										$child_categories=$tbCategories->getLeafCategoriesByParent($category);//get tất cả nút con
										$count=0;
										$dataProducts=array();
										if($child_categories!=null)
										{
											foreach($child_categories as $child_category)
											{
										
												$products=$tbThings->getTypedThingsByCategory($child_category['category_id'],3);
												if(count($products)>0)
												{
													foreach($products as $product)
														array_push($dataProducts,$product);
												}
												$count++;
											}
										}
									}
								
									//End Edit
									$type="QA_category";
									}
									else 
									{
									$this->_redirect('/index/index');
									}
								}
						}
					}
					else 
					{
						$this->_redirect('/index/index');
					}
				}
				else
				{
					$dataProducts=$tbThings->$tbThings->getAllThingsbyType(1);
						$type="all";
				}
			if($dataProducts!=null)
			{
				//Start------Get số lượng Products
				$countProducts=count($dataProducts);
				
				//Start------Kiểm tra trang
				if (!isset($_GET["page"]) || is_numeric($_GET["page"])!=true || $_GET["page"]<0) 
				$page = 1;
		    else 
		        $page = $_GET["page"];
		        
			   // khoi tao phan trang
		       $limit=10;
		        switch ($type) {
		        	case "news":{
		        		$this->pagination->init("?show=2&type=".$loai."&page=",$countProducts,$limit,$page);
		        		
		        		break;
		        	}
		        	case "news_category":{
		        		$this->pagination->init("?show=2&type=".$loai."&category=".$category."&page=", $countProducts, $limit, $page);
		        		break;
		        	
		        	}
		        	case "QA":{
		        		$this->pagination->init("?show=2&type=".$loai."&page=",$countProducts,$limit,$page);
		        		
		        		break;
		        	}
		        case "QA_category":{
		        		$this->pagination->init("?show=2&type=".$loai."&category=".$category."&page=",$countProducts,$limit,$page);
		        		
		        		break;
		        	}
		        	case "product_category":{
		        		$this->pagination->init("?show=2&type=".$loai."&category=".$category."&page=", $countProducts, $limit, $page);
		        		break;
		        	}
		        	
		        	default:{
		        		$this->pagination->init("?show=2&page=", $countProducts, $limit, $page);
		        		
		        		break;
		        	}
		        	
		        }
			//End--------Kiểm tra trang
			$this->view->pagination = $this->pagination->render();
			switch ($type)
				{
					case "all":
						{
							$dataProducts=$tbThings->getThingsInPage(1,$page,$limit);
							break;
						}
					case "news":
						{
							
						}
					
					case "QA":
						{
							$dataProducts=$tbThings->getThingsInPage($loai,$page, $limit);
							break;
						}
					case "news_category":
						{
						}	
					case "QA_category":
						{
							
						}	
					
					case "product_category":
						{
							$temp=$dataProducts;
							$dataProducts=array();
							for($i=($page-1)*$limit;$i<$page*$limit;$i++)
							{
								if($i>=count($temp))
									break;
								array_push($dataProducts,$temp[$i]);
							}
							break;
						}
				}
			
			//format multi-language
			switch ($type)
			{
				case "all":{}
				case "product_category":
				{
					$i=0;
					if($dataProducts!=null)
					{
						foreach($dataProducts as $product)
						{
							$description_lang=unserialize($product["short_description"]);
							if(isset($description_lang[$name['name']]))
								$product["short_description"]=$description_lang[$name['name']];
							else
								$product["short_description"]="-------------------";
								
							$Products[$i]=array(
							"thing_id"=>$product["thing_id"],
							"title"=>$product["title"],
							"short_description"=>$product["short_description"],
							"price"=>$product["price"],
							"update_date" =>$dateInt->makeDays($product['update_date']),
							"avatar"=>$product["avatar"],
							);
							$i++;
						}
					}
					break;
				}
				case "news":{}
				case "QA":{}
				case "QA_category":{}
				case "news_category":
				{
					$i=0;
					if($dataProducts!=null)
					{
						foreach($dataProducts as $product)
						{
							$description_lang=unserialize($product["short_description"]);
							$title_lang=unserialize($product["title"]);
							if(isset($description_lang[$name['name']]))
								$product["short_description"]=$description_lang[$name['name']];
							else
								$product["short_description"]="-------------------";
							if(isset($title_lang[$name['name']]))
								$product["title"]=$title_lang[$name['name']];
							else
								$product["title"]="-------------------";
							
							$Products[$i]=array(
							"thing_id"=>$product["thing_id"],
							"title"=>$product["title"],
							"short_description"=>$product["short_description"],
							"price"=>$product["price"],
							"update_date" =>$dateInt->makeDays($product['update_date']),
							"avatar"=>$product["avatar"],
							);
							$i++;
						}
					}
					break;
				}
			}
			//End--------get thing và phân trang
			
			//Start------Truyền parameter qua View
				switch ($type)
				{
					
					case "news":
						{
							
							
						}
				case "QA":
						{
							
							
						}
				case "QA_category":
						{
							
							
						}
					case "news_category":
						{
							
						}
					
				case "all":
						{
							
						
						}
				case "product_category":
						{
							$this->view->Products=$Products;
							break;
						}
				}
			}
			
			
			
		}
		/////////////////////////////////////////////////////////////////////////
		//                                                                     //
		/////////////////////////////////////////////////////////////////////////
		else // show dang thumb
		{
			//Start------Khai báo các parameter
			$tbCategories = new ListShow_Model_Categories();
			$tbThings = new ListShow_Model_Things();
			//End--------Khai báo các parameter
			
			$page = 1;
			$limit = 6;
			$type = 1;
			$getthings = 0;
			$category = 0;
			$countThings = 0;
			
			//Start------Kiểm tra trang
			if (!isset($_GET["page"]) || is_numeric($_GET["page"])!=true || $_GET["page"]<0) 
				$page = 1;
		    else 
		        $page = $_GET["page"];
			//End--------Kiểm tra trang
			
			//Start------Kiểm tra category và type things
			if(count($_GET) && isset($_GET["category"]) && is_numeric($_GET["category"]) && $_GET["category"]>0)
			{	
				$category = $_GET["category"];
				
				if(count($_GET) && isset($_GET["type"]) && is_numeric($_GET["type"]) && $_GET["type"]>0)
				{
					$type = $_GET["type"];
				}
			}
			else
			{
				if (count($_GET) && isset($_GET["type"]) && is_numeric($_GET["type"]) && $_GET["type"]>0)
				{
					$type = $_GET["type"];
				}
			}
			//Start------Kiểm tra category và type things
			
			//Start------get thing và phân trang
			if($category == 0)
			{
				//Start------count all things
				$countThings = count($tbThings->getAllThingsbyType($type));
				//End--------count lượng all things
				
				//Start------get things in page
				$getthings = $tbThings->getThingsInPage($type,$page,$limit);
				//End--------get things in page
				
				//Start------Khởi tạo phân trang
				if($type == 1)
					$this->pagination->init("?show=3&page=", $countThings, $limit, $page);
				else
					$this->pagination->init("?show=3&type=".$type."&page=", $countThings, $limit, $page);
				//End--------Khởi tạo phân trang
			}
			else
			{
				$category_info = $tbCategories->getCategoriesRowbyId($category);
				if(count($category_info)>0) $this->view->cate_info = $category_info;
								
				//Start------get name of category
				$nameCate = unserialize($category_info['category_name']);
				if(isset($nameCate[$name['name']]) && isset($name)){
					$category_name = $nameCate[$name['name']];
				}else{
					$category_name = $nameCate['Default'];
				}
				$this->view->cate_name = $category_name;
				//End--------get name of category
				if(($category_info['is_leaf'] == 1)&&($category_info['parent_id'] != 0))//Nếu là nút lá
				{	
					$this->view->categoryleaf = $category;
					
					//Start------count things
					$countThings = count($tbThings->getTypedThingsByCategory($category,$type));
					//End--------count things
					
					//Start------get things in page
					$getthings = $tbThings->getThingsByCategoryInPage($category,$type,$page,$limit);
					//End--------get things in page
				}
				else
				{
					//Start------count things
					$countThings = count($tbThings->getTypedThingsByParentCategory($category,$type));
					//End--------count things
				
					//Start------get things in page
					$getthings = $tbThings->getThingsByParentCategoryInPage($category,$type,$page,$limit);
					//End--------get things in page				
				}	
				
				//Start------Khởi tạo phân trang
				if($type == 1)
					$this->pagination->init("?show=3&category=".$category."&page=", $countThings, $limit, $page);
				else
					$this->pagination->init("?show=3&category=".$category."&type=".$type."&page=", $countThings, $limit, $page);
				//End--------Khởi tạo phân trang
				
			}
			//End--------get thing và phân trang
			
			//format multi-language
			switch ($type)
			{
				case "1":
				{
					$i = 0;
					if($getthings!=null)
					{
						foreach($getthings as $product)
						{
							$description_lang=unserialize($product["short_description"]);
							if(isset($description_lang[$name['name']]))
								$getthings[$i]["short_description"]=$description_lang[$name['name']];
							else
								$getthings[$i]["short_description"]="-------------------";
							$i++;
						}
					}
					break;
				}
				case "2":{}
				case "3":
				{
					$i = 0;
					if($getthings!=null)
					{
						foreach($getthings as $product)
						{
							$description_lang=unserialize($product["short_description"]);
							$title_lang=unserialize($product["title"]);
							if(isset($description_lang[$name['name']]))
								$getthings[$i]["short_description"]=$description_lang[$name['name']];
							else
								$getthings[$i]["short_description"]="-------------------";
							if(isset($title_lang[$name['name']]))
								$getthings[$i]["title"]=$title_lang[$name['name']];
							else
								$getthings[$i]["title"]="-------------------";
							$i++;
						}
					}
					break;
				}
			}
			
			//Start------Truyền parameter qua View
			if($getthings != null)
				$this->view->getthings = $getthings;
			else
				$this->view->getthings = null;
				
			$this->view->pagination = $this->pagination->render();
			$this->view->type = $type;
			//End--------Truyền parameter qua View
		}
		
		switch ($_GET["type"])
		{
			case 1: $this->view->route = 'index';break;
			case 2: $this->view->route = 'news';break;
			case 3: $this->view->route = 'qa';break;
			default : $this->view->route = 'index';
		}
	}	

}